﻿#include "mainwindow.h"
#include <QDesktopWidget>
#include <QScreen>
#include <QButtonGroup>
#include <QDateTime>
#include "ui_mainwindow.h"

#include <QBitmap>
#include <QMessageBox>
#include "dialog_log.h"
#include "net_mgr.h"
#include "epowerapp.h"
#include "form_info.h"
#include "form_alert.h"
#include "form_health.h"
#include "form_log.h"
#include "form_health.h"

#include "form_setting.h"
#include "com_manager.h"



#ifdef Q_CC_MSVC
#pragma execution_character_set("utf-8")
#endif


enum form_
{
	_Info,
	_Alert,
     _Health,
    _Log,
	_Setting,
	_Control
};

MainWindow::MainWindow(QWidget* parent)
	: QMainWindow(parent)
	, ui(new Ui::MainWindow)
	, upd_server(NULL)
	, tcp_client(NULL)
{
	ui->setupUi(this);
	setMouseTracking(true);//跟踪鼠标

    //setWindowTitle("电气综合管理单元样机软件  V1.0.0.6");
    setWindowTitle(qApp->applicationName() + "    V" + qApp->applicationVersion());

	QString btn_style = "QPushButton{border-image:url(:/UI/UI/btn_bg.png);}"
		"QPushButton:hover{border-image:url(:/UI/UI/btn_selected.png);}"
		"QPushButton:checked{border-image:url(:/UI/UI/btn_selected.png);}"
		"QPushButton:pressed{border-image:url(:/UI/UI/btn_selected.png);}";

	QPixmap pixmap(":/UI/UI/btn_bg.png");
	QBitmap mask = pixmap.mask();

	ui->button_info->setStyleSheet(btn_style);
	//ui->button_info->setMask(mask);
	ui->button_info->setCheckable(true);

	ui->button_alert->setStyleSheet(btn_style);
	//ui->button_alert->setMask(mask);
	ui->button_alert->setCheckable(true);

    ui->button_health->setStyleSheet(btn_style);
	//ui->button_statistic->setMask(mask);
    ui->button_health->setCheckable(true);

	ui->button_log->setStyleSheet(btn_style);
	//ui->button_log->setMask(mask);
	ui->button_log->setCheckable(true);

	//ui->button_health->setStyleSheet(btn_style);
	//ui->button_health->setMask(mask);
	//ui->button_health->setCheckable(true);

	ui->button_setting->setStyleSheet(btn_style);
	//ui->button_setting->setMask(mask);
	ui->button_setting->setCheckable(true);

	//ui->button_control->setStyleSheet(btn_style);
	//ui->button_control->setMask(mask);
	//ui->button_control->setCheckable(true);

	QButtonGroup* group = new QButtonGroup;
	group->addButton(ui->button_info);
	group->addButton(ui->button_alert);
    group->addButton(ui->button_health);
	group->addButton(ui->button_log);
	group->addButton(ui->button_setting);
	//	group->addButton(ui->button_control);

	group->setExclusive(true);

	ui->frame->setObjectName("backLabel");
	ui->frame->setStyleSheet("QFrame#backLabel{border-image:url(:/UI/main_bg.png);}");

	ui->frame_top->setStyleSheet("background-color: rgba(0, 0, 0,0 );");

	ui->stackedWidget->insertWidget(_Info, new FormInfo);
	ui->stackedWidget->insertWidget(_Alert, new FormAlert);
    ui->stackedWidget->insertWidget(_Health, new FormHealth);
    ui->stackedWidget->insertWidget(_Log, new FormLog);
    ui->stackedWidget->insertWidget(_Setting, new FormSetting);

	ui->stackedWidget->setStyleSheet("background-color: rgba(0,9,52,128 );");

	ui->stackedWidget->setCurrentIndex(0);

	// Dialog_Log* dlg = new Dialog_Log(this);

	// int desktopCount = QApplication::desktop()->screenCount();
	// if (desktopCount == 2) {
	// 	// 获取第二个显示器的信息
	// 	QRect rect = QApplication::desktop()->screenGeometry(1);
	// 	// 移动窗口到第二个屏幕
	// 	//move(rect.left(), rect.top());

	// 	dlg->move(rect.left(), rect.top());
	// }

	this->setFixedWidth(1920);
	this->setFixedHeight(1080);

	//showFullScreen();
	this->showMaximized();


	//dlg->show();

    connect((const EPowerApp*)qApp, &EPowerApp::SendCurrentMousePoint, [=](QPoint  pt) {

        QPointF mouse_pt = this->mapFromGlobal(pt);
        QString mouse_info = QString("mouse pos: %1, %2").arg(mouse_pt.x()).arg(mouse_pt.y());

        statusBar()->showMessage(mouse_info);

     //   status_date->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz"));
    });
init_status_timer();
}

MainWindow::~MainWindow()
{

    com_manager::instance()->close();
	delete ui;
}


void MainWindow::init_status_timer()
{
    QLabel* status_date = new QLabel("yyyy-MM-dd hh:mm:ss zzz");
    statusBar()->addPermanentWidget(status_date);

    QTimer* timer = new QTimer;
    //connect(timer, SIGNAL(timeout()), this, SLOT(timeUpdate()));
    connect(timer, &QTimer::timeout, [=]  {

     //   QPointF mouse_pt = this->mapFromGlobal(pt);
      //  QString mouse_info = QString("mouse pos: %1, %2").arg(mouse_pt.x()).arg(mouse_pt.y());

       // statusBar()->showMessage(mouse_info);

        status_date->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz"));
    });
   timer->start(15);
}

//void MainWindow::timeUpdate()
//{
//    QDateTime CurrentTime = QDateTime::currentDateTime();
//    QString strTime = CurrentTime.toString(" yyyy年MM月dd日 hh:mm:ss zzz"); //设置显示的格式
//    ui->label_datetime->setText(strTime);
//    //statusBar()->addPermanentWidget(ShowTimeLabel);
//    //statusBar()->setSizeGripEnabled(true); //设置是否显示右边的大小控制点
//}
void MainWindow::on_button_info_clicked()
{
	//QMessageBox::information(this, "", "工况展示");

	ui->stackedWidget->setCurrentIndex(form_::_Info);
}


void MainWindow::on_button_alert_clicked()
{
	//QMessageBox::information(this, "", "故障报警");

	ui->stackedWidget->setCurrentIndex(form_::_Alert);

	//	ui->button_alert->setChecked(true);
}


void MainWindow::on_button_health_clicked()
{
	//QMessageBox::information(this, "", "统计报表");
    ui->stackedWidget->setCurrentIndex(form_::_Health);

}


void MainWindow::on_button_log_clicked()
{
	//QMessageBox::information(this, "", "日志管理");
	ui->stackedWidget->setCurrentIndex(form_::_Log);

}


void MainWindow::on_button_control_clicked()
{
	//	QMessageBox::information(this, "", "控制功能");
	ui->stackedWidget->setCurrentIndex(form_::_Control);

}


void MainWindow::on_button_setting_clicked()
{

	ui->stackedWidget->setCurrentIndex(form_::_Setting);

}

void MainWindow::mousePressEvent(QMouseEvent* event)
{
	QRect  rect = this->childrenRect();
	QRect exit_rect(rect.right() - 120, 16, 100, 64);
	if (exit_rect.contains(event->pos()))
	{
		//if (0 == QMessageBox::warning(this, "样机", " 确定退出？ ", "确定", "取消"))
		//{
		//	event->accept();
		//	this->close();
		//}
		//else
		//	event->ignore();
	}
}


void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
	QString mouse_info = QString("mouse pos: %1, %2").arg(event->pos().x()).arg(event->pos().y());

	statusBar()->showMessage(mouse_info);
}


bool MainWindow::event(QEvent* event)
{// 事件类型是移动鼠标
	if (event->type() == QEvent::MouseMove)
	{
		QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);

		QString mouse_info = QString("mouse pos: %1, %2").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y());

		statusBar()->showMessage(mouse_info);
		//QPointF p = mouseEvent->pos(); // 获取鼠标位置
		//QPoint currentMousePoint = p.toPoint();
		//emit SendCurrentMousePoint(currentMousePoint); // 发送信号到主界面
	}
	return QWidget::event(event);
}






void MainWindow::closeEvent(QCloseEvent* event)
{
    if (0 == QMessageBox::warning(this, qApp->applicationName(), " 确定退出？ ", "确定", "取消"))
    {
        event->accept();
        this->close();
    }
    else
        event->ignore();

}
